import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Observer;
import java.util.Set;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.JSeparator;
import java.awt.Choice;
import java.awt.TextArea;
import java.awt.List;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.util.Observable;
import java.awt.Button;
import javax.swing.JList;
import javax.swing.DropMode;
import javax.swing.DefaultListModel;


public class VueSaisieParution extends Vue{
	
	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JTextField textFieldIssn;
	private JTextField textFieldNomPer;
	private JTextField textFieldIdPar;
	private JButton btnAdd;
	private JButton btnSuppr;
	private JButton buttonAnnuler;
	private JButton btnEnregistrer;
	private JButton btnCrerArticle;
	private JButton btnRechercher;
	
	private JList listArticlesAdded;
	private DefaultListModel articlesAdded;
	
	private JList listArticles;
	private DefaultListModel articles;
	

	
	/* Create the frame.*/
	 
	public VueSaisieParution(Controleur controleur, HashMap<String, Article> art) 
	{
		super (controleur);
		articles = new DefaultListModel();
		articlesAdded = new DefaultListModel();
		for(String titre : art.keySet())
		{
			articles.addElement(titre);
		}
		
		setTitle("Enregistrement d'une nouvelle parution");
		setBounds(100, 100, 471, 421);
		contentPane = new JPanel();
		this.addWindowListener (new WindowAdapter() 
		{
			public void windowClosing (WindowEvent e) 
			{
				getControleur().fermerVue(VueSaisieParution.this);
			}
		});
		
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		//Label Issn + son champs de texte
		JLabel lblNewLabel = new JLabel("Issn");
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel.setBounds(75, 32, 43, 17);
		contentPane.add(lblNewLabel);
		
		textFieldIssn = new JTextField();
		textFieldIssn.setBounds(146, 30, 164, 19);
		contentPane.add(textFieldIssn);
		textFieldIssn.setColumns(10);
		
		//Label Nom + son champs de texte
		JLabel lblNewLabel_2 = new JLabel("Nom");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_2.setBounds(57, 105, 61, 15);
		contentPane.add(lblNewLabel_2);
		
		textFieldNomPer = new JTextField();
		textFieldNomPer.setEditable(false);
		textFieldNomPer.setBounds(146, 102, 164, 19);
		contentPane.add(textFieldNomPer);
		textFieldNomPer.setColumns(10);	

		//separateur
		JSeparator separator = new JSeparator();
		separator.setBounds(0, 150, 532, 2);
		contentPane.add(separator);
		
		//Label + champs de texte
		JLabel lblIdentifiantDeLa = new JLabel("Id parution");
		lblIdentifiantDeLa.setHorizontalAlignment(SwingConstants.RIGHT);
		lblIdentifiantDeLa.setBounds(-13, 163, 131, 17);
		contentPane.add(lblIdentifiantDeLa);		
		textFieldIdPar = new JTextField();
		textFieldIdPar.setBounds(146, 161, 164, 19);
		contentPane.add(textFieldIdPar);
		textFieldIdPar.setColumns(10);
		
		
		btnAdd = new JButton(">");
		btnAdd.setBounds(205, 234, 61, 25);
		btnAdd.setEnabled(false);
		contentPane.add(btnAdd);
		btnAdd.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				Object[] s = listArticles.getSelectedValues();
				addArticleParution(s);
			}
		});
		
		
		btnSuppr = new JButton("<");
		btnSuppr.setBounds(205, 270, 61, 25);
		btnSuppr.setEnabled(false);
		contentPane.add(btnSuppr);
		btnSuppr.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				Object[] s = listArticlesAdded.getSelectedValues();
				removeArticleParution(s);
			}
		});
		
		
		listArticles = new JList();
		JScrollPane scrollPane = new JScrollPane(listArticles);
		scrollPane.setBounds(40, 217, 129, 102);
		contentPane.add(scrollPane);
		listArticles.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		listArticles.setLayoutOrientation(JList.VERTICAL);
		listArticles.setVisibleRowCount(-1);
		listArticles.addListSelectionListener(new ListSelectionListener()
		{
			public void valueChanged(ListSelectionEvent e)
			{
				if(listArticles.isSelectionEmpty())
					btnAdd.setEnabled(false);
				else
					btnAdd.setEnabled(true);
			}
		});
		
		
		listArticlesAdded = new JList();
		JScrollPane scrollPaneAdded = new JScrollPane(listArticlesAdded);
		scrollPaneAdded.setBounds(298, 217, 129, 102);
		contentPane.add(scrollPaneAdded);
		listArticlesAdded.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		listArticlesAdded.setLayoutOrientation(JList.VERTICAL);
		listArticlesAdded.setVisibleRowCount(-1);
		listArticlesAdded.addListSelectionListener(new ListSelectionListener()
		{
			public void valueChanged(ListSelectionEvent e)
			{
				if(listArticlesAdded.isSelectionEmpty())
					btnSuppr.setEnabled(false);
				else
					btnSuppr.setEnabled(true);
			}
		});
		
		btnEnregistrer = new JButton("Enregistrer");
		btnEnregistrer.setBounds(320, 330, 107, 25);
		contentPane.add(btnEnregistrer);
		btnEnregistrer.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				getControleur().nouvParution((textFieldIssn.getText()), textFieldIdPar.getText(), articlesAdded.toArray());
			}
		});		
		
		btnCrerArticle = new JButton("Créer article");
		btnCrerArticle.setBounds(40, 330, 114, 25);
		contentPane.add(btnCrerArticle);
		btnCrerArticle.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				getControleur().saisirArticle();
			}
		});		
		
		buttonAnnuler = new JButton("Annuler");
		buttonAnnuler.setBounds(320, 64, 107, 25);
		contentPane.add(buttonAnnuler);
		buttonAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().fermerVue(VueSaisieParution.this);}
		});
		
		btnRechercher = new JButton("Rechercher");		
		btnRechercher.setBounds(320, 28, 107, 25);
		contentPane.add(btnRechercher);
		
		JLabel lblArticlesEnBase = new JLabel("Articles en BDD");
		lblArticlesEnBase.setBounds(57, 191, 151, 17);
		contentPane.add(lblArticlesEnBase);
		
		JLabel lblArticlesAjoutsA = new JLabel("Articles ajoutés ");
		lblArticlesAjoutsA.setHorizontalAlignment(SwingConstants.CENTER);
		lblArticlesAjoutsA.setBounds(286, 192, 147, 14);
		contentPane.add(lblArticlesAjoutsA);
		btnRechercher.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) {
			getControleur().rechPeriodique(textFieldIssn.getText());}
			});
		
		refresh();
	}	
		
	private void addArticleParution(Object[] s)
	{
		for(int i =0; i< s.length ; i++)
		{
			articlesAdded.addElement(s[i]);
			articles.removeElement(s[i]);
		}
		refresh();
	}
	
	private void removeArticleParution(Object[] s)
	{
		for(int i =0; i< s.length ; i++)
		{
			articlesAdded.removeElement(s[i]);
			articles.addElement(s[i]);
		}
		refresh();
	}

	public void alimente(Periodique per, HashMap<String,Article> articles)
	{
		HashMap<String,Article> arts = new HashMap<String,Article>(articles);
		DefaultListModel listModel = new DefaultListModel();
		if(textFieldNomPer.getText()!=per.getNom())
			textFieldNomPer.setText(per.getNom());
		else
		{
			for(int i=0;i<listArticlesAdded.getModel().getSize();i++)
			{
				if(arts.containsKey(listArticlesAdded.getModel().getElementAt(i)))
					arts.remove(listArticlesAdded.getModel().getElementAt(i));
			}
		}
		
		for(String s : articles.keySet())
		{
			listModel.addElement(s);
		}
		listArticles.setModel(listModel);
		refresh();
	}
	
	public void refresh()
	{
		listArticles.setModel(articles);
		listArticlesAdded.setModel(articlesAdded);
	}
	
	public void update(Article newArticle)
	{
		articles.addElement(newArticle.getTitre());
		refresh();
	}
	
	/**
	 * définition des états de la fenêtre, cad des régions actives selon l'état
	 */
	public void setEtat (int etat){
		switch (etat) {
		case initiale: {
			btnRechercher.setEnabled(true);
			buttonAnnuler.setEnabled(true);
			btnEnregistrer.setEnabled(false);
			btnAdd.setEnabled(false);
			btnSuppr.setEnabled(false);
			listArticlesAdded.setEnabled(false);
			textFieldNomPer.setText("");
			textFieldIssn.setText("");
			textFieldIdPar.setText("");
			textFieldIdPar.setEditable(false);
			break;
			}
		case finale: {
			btnRechercher.setEnabled(true);
			buttonAnnuler.setEnabled(true);
			btnEnregistrer.setEnabled(true);
			listArticlesAdded.setEnabled(true);
			btnAdd.setEnabled(false);
			btnSuppr.setEnabled(false);
			textFieldIdPar.setEditable(true);
			break;
			}
		}
	}
}
